package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.bo.CartCheckedBo;
import com.cskaoyan.app.bean.bo.CartCheckoutBo;
import com.cskaoyan.app.bean.bo.CartCheckoutRequestBo;
import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.bean.vo.CartDataVo;
import com.cskaoyan.app.bean.vo.CartTotalVo;
import com.cskaoyan.app.mapper.MarketAddressMapper;
import com.cskaoyan.app.mapper.MarketCartMapper;
import com.cskaoyan.app.mapper.MarketCouponMapper;
import com.cskaoyan.app.mapper.MarketCouponUserMapper;
import com.cskaoyan.app.util.CartTotalUtil;
import com.cskaoyan.app.wxservice.WxCartService;
import com.cskaoyan.app.util.StringsUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 廖平安
 * @CreateTime: 2022-12-31  15:47
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class WxCartServiceImpl implements WxCartService {
    @Autowired
    private MarketCartMapper cartMapper;
    @Autowired
    private MarketCouponMapper couponMapper;
    @Autowired
    private MarketAddressMapper addressMapper;
    @Autowired
    private MarketCouponUserMapper couponUserMapper;

    @Override
    public Integer add(Map map, Integer userId) {
        //获取参数
        Integer goodsId = (Integer) map.get("goodsId");
        Integer productId = (Integer) map.get("productId");
        int number1 = (int) map.get("number");

        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andProductIdEqualTo(productId);
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCart = cartMapper.selectByExample(example);

       /* Boolean flag = false;
        for (MarketCart cart : market) {
            number1 += cart.getNumber();
            flag = true;
        }*/

        //判断购物车里是否有相同的商品（规格也一样）
        if (marketCart.size() != 0) {
            //有相同商品，只对数量做更改
            Short number2 = marketCart.get(0).getNumber();
            int number3 = number2 + number1;
            MarketCart market = new MarketCart();
            Short number = (short) number3;
            market.setNumber(number);
            cartMapper.updateByExampleSelective(market, example);
        } else {
            //没有相同规格商品，做插入
            MarketCart cart = cartMapper.selectCart(goodsId,productId);
            Short number = (short) number1;
            cart.setUserId(userId);
            cart.setNumber(number);
            cart.setAddTime(new Date());
            cart.setUpdateTime(new Date());
            cartMapper.insertSelective(cart);
        }
        Integer data = cartMapper.selectData();
        return data;
    }

    @Override
    public CartDataVo index() {
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        Subject subject = SecurityUtils.getSubject();
        Integer userid = (Integer) subject.getSession().getAttribute("userid");
        criteria.andUserIdEqualTo(userid);
        List<MarketCart> list = cartMapper.selectByExample(example);
        CartTotalVo total = CartTotalUtil.getData(list);
        return new CartDataVo(list, total);

    }

    @Override
    public void update(Map map) {
        Integer id = (Integer) map.get("id");
        int number1 = (int) map.get("number");
        Short number = (short) number1;
        cartMapper.updateCart(id, number);
    }

    @Override
    public CartDataVo checked(CartCheckedBo checkedBo) {
        List<Integer> productIds = Arrays.asList(checkedBo.getProductIds());
        //?????????
        Integer isChecked = checkedBo.getIsChecked();
        Boolean checked;
        if (isChecked == 1) {
            checked = true;
        } else {
            checked = false;
        }
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdIn(productIds);
        criteria.andDeletedEqualTo(false);
        MarketCart marketCart = new MarketCart();
        marketCart.setChecked(checked);
        cartMapper.updateByExampleSelective(marketCart, example);
        MarketCartExample example1 = new MarketCartExample();
        example1.createCriteria().andDeletedEqualTo(false);
        List<MarketCart> list = cartMapper.selectByExample(example1);
        CartTotalVo total = CartTotalUtil.getData(list);
        return new CartDataVo(list, total);
    }

    @Override
    public CartDataVo delete(CartCheckedBo checkedBo) {
        List<Integer> productIds = Arrays.asList(checkedBo.getProductIds());
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdIn(productIds);
        MarketCart marketCart = new MarketCart();
        marketCart.setDeleted(true);
        cartMapper.updateByExampleSelective(marketCart, example);
        MarketCartExample example1 = new MarketCartExample();
        example1.createCriteria().andDeletedEqualTo(false);
        List<MarketCart> list = cartMapper.selectByExample(example1);
        CartTotalVo total = CartTotalUtil.getData(list);
        return new CartDataVo(list, total);
    }

    @Override
    public Integer goodsCount() {
        Integer data = cartMapper.selectData();
        return data;
    }

    @Override
    public Integer fastAdd(Map map, Integer userId) {
        Integer goodsId = (Integer) map.get("goodsId");
        Integer productId = (Integer) map.get("productId");
        int number1 = (int) map.get("number");
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andProductIdEqualTo(productId);
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCart = cartMapper.selectByExample(example);
        Short number = (short) number1;

        //更改购物车里所有的checked为false
        MarketCartExample marketCartExample1 = new MarketCartExample();
        marketCartExample1.createCriteria().andDeletedEqualTo(false);
        MarketCart marketCart1 = new MarketCart();
        marketCart1.setChecked(false);
        cartMapper.updateByExampleSelective(marketCart1, marketCartExample1);

        //判断购物车里是否有相同的商品（规格也一样）
        if (marketCart.size() != 0) {
            //有相同商品，只对数量做更改
            MarketCart market = new MarketCart();
            Short number2 = (short) number1;
            market.setNumber(number2);
            cartMapper.updateByExampleSelective(market, example);
            MarketCart market1 = marketCart.get(0);
            Integer id = market1.getId();
            MarketCartExample marketCartExample2 = new MarketCartExample();
            marketCartExample2.createCriteria().andIdEqualTo(id);
            MarketCart marketCart2 = new MarketCart();
            marketCart2.setChecked(true);
            cartMapper.updateByExampleSelective(marketCart2, marketCartExample2);
            return id;
        } else {
            //没有相同规格商品，做插入
            MarketCart cart = cartMapper.selectCart(goodsId,productId);
            cart.setUserId(userId);
            cart.setNumber(number);
            cart.setAddTime(new Date());
            cart.setUpdateTime(new Date());
            cartMapper.insertSelective(cart);
            return cart.getId();
        }
    }

    @Override
    public CartCheckoutBo checkout(CartCheckoutRequestBo checkoutRequestBo) {
        Integer cartId = checkoutRequestBo.getCartId();
        Integer addressId = checkoutRequestBo.getAddressId();
        Integer couponId = checkoutRequestBo.getCouponId();
        Integer userCouponId = checkoutRequestBo.getUserCouponId();
        Integer grouponRulesId = checkoutRequestBo.getGrouponRulesId();
        Subject subject = SecurityUtils.getSubject();
        Integer userid = (Integer) subject.getSession().getAttribute("userid");

        CartCheckoutBo checkoutBo = new CartCheckoutBo();

        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andCheckedEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = cartMapper.selectByExample(example);
        CartTotalVo totalVo = CartTotalUtil.getData(marketCarts);
        Double amount = totalVo.getCheckedGoodsAmount();

        double discount = 0.0;
        if (couponId != 0 && couponId != -1) {
            MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(couponId);
            discount = marketCoupon.getDiscount().doubleValue();
        }

        checkoutBo.setCartId(cartId);
        checkoutBo.setAddressId(addressId);
        checkoutBo.setGoodsTotalPrice(amount);
        checkoutBo.setActualPrice(amount - discount);
        checkoutBo.setOrderTotalPrice(amount - discount);
        checkoutBo.setCouponId(couponId);
        checkoutBo.setCouponPrice(discount);
        checkoutBo.setUserConponId(userCouponId);
        checkoutBo.setGrouponRulesId(grouponRulesId);
        checkoutBo.setFreightPrice(0.0);
        checkoutBo.setGrouponPrice(0.0);

        MarketAddress marketAddress = null;
        if (addressId != 0) {
            marketAddress = addressMapper.selectByPrimaryKey(addressId);
        }
        checkoutBo.setCheckedAddress(marketAddress);

        MarketCouponUserExample example1 = new MarketCouponUserExample();
        example1.createCriteria().andUserIdEqualTo(userid);
        example1.createCriteria().andDeletedEqualTo(false);
        int count = (int) couponUserMapper.countByExample(example1);
        checkoutBo.setAvailableCouponLength(count);


        //List<MarketCart> list = cartMapper.selectByExample(example);
        checkoutBo.setCheckedGoodsList(marketCarts);
        return checkoutBo;

    }
}
